package com.leetcode.周赛.第234场;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: xiaomi
 * @date: 2021/3/28
 * @description: 5714. 替换字符串中的括号内容
 * https://leetcode-cn.com/contest/weekly-contest-234/problems/evaluate-the-bracket-pairs-of-a-string/
 */
public class B_5714_替换字符串中的括号内容 {

    static B_5714_替换字符串中的括号内容 action = new B_5714_替换字符串中的括号内容();

    public static void main(String[] args) {
        String s = "(name)is(age)yearsold";
        s = "hi(ame)";
        List<String> list1 = Arrays.asList("name", "bob");
        List<String> list2 = Arrays.asList("age", "two");
        List<List<String>> knowledge = new ArrayList<>();
        knowledge.add(list1);
        knowledge.add(list2);
        String res = action.evaluate(s, knowledge);
        System.out.println("res = " + res);
    }

    /**
     * 没想到今天这么考验正则表达式
     *
     * @param s
     * @param knowledge
     * @return
     */
    public String evaluate(String s, List<List<String>> knowledge) {
        //1.map
        HashMap<String, String> map = new HashMap<>();
        for (List<String> list :
                knowledge) {
            map.put(list.get(0), list.get(1));
        }

        Pattern pattern = Pattern.compile("\\(([a-z]+)\\)");
        Matcher matcher = pattern.matcher(s);
        //index - str
        //TreeMap<Integer, String> indexMap = new TreeMap<>();

        PriorityQueue<Item> heap = new PriorityQueue<>(new Comparator<Item>() {
            @Override
            public int compare(Item o1, Item o2) {
                return o1.index - o2.index;
            }
        });
        while (matcher.find()) {
            //indexMap.put(matcher.start(), matcher.group(1));
            heap.add(new Item(matcher.start(), matcher.group(1)));
        }
        LinkedList<Character> queue = new LinkedList<>();
        char[] chars = s.toCharArray();
        int len = chars.length;
        for (int i = 0; i < len; i++) {
            if (!heap.isEmpty() && i == heap.peek().index) {
                //need replace
                Item remove = heap.remove();
                String tempStr =map.get(remove.value);
                if (tempStr==null){
                    queue.addLast('?');
                }else {
                    char[] tempChars = tempStr.toCharArray();
                    int tempLen = tempChars.length;
                    for (int j = 0; j < tempLen; j++) {
                        queue.addLast(tempChars[j]);
                    }

                }
                i += remove.value.length() + 1;
            } else {
                queue.addLast(chars[i]);
            }
        }

        char[] resChars = new char[queue.size()];
        int index=0;
        while (!queue.isEmpty()){
            resChars[index++]=queue.removeFirst();
        }
        return new String(resChars);
    }

    static class Item{
        Integer index;
        String value;

        public Item(Integer index, String value) {
            this.index = index;
            this.value = value;
        }
    }
}
